<?php

namespace App\Jobs;

use Xu42\Qznjw2014\Qznjw2014 as EducationSystem;


class SaveStudentUserInfoJob extends Job
{

    protected $username;
    protected $password;
    protected $openid;
    protected $isRegister;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct( $username, $password, $openid, $isRegister )
    {
        $this->username   = $username;
        $this->password   = $password;
        $this->openid     = $openid;
        $this->isRegister = $isRegister;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $educationSystem = new EducationSystem( $this->username, $this->password );

        $eduInfo = $educationSystem->userinfo();

//        if ( $this->isRegister ) {
//            $modelUserinfo = \App\Models\Userinfo::whereUsername( $this->username )->first();
//
//            $modelUserinfo->username = $this->username;
//            $modelUserinfo->openid   = $this->openid;
//            $modelUserinfo->edu_info = json_encode( $eduInfo );
//        } else {
            $modelUserinfo = new \App\Models\Userinfo;

            $modelUserinfo->username = $this->username;
            $modelUserinfo->openid   = $this->openid;
            $modelUserinfo->edu_info = json_encode( $eduInfo );
//        }
        $modelUserinfo->save();
    }
}
